package world.criver.h50909.tool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import world.criver.h50909.data.daSqlite;
import world.criver.h50909.entity.entConfig;
import world.criver.h50909.function.funSaveImage;
import world.criver.h50909.properties.proConfig;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Base64;
import java.util.List;
@Component
public class toolConfig {
    @Autowired
    private proConfig proConfig;
    @Autowired
    private funSaveImage funSaveImage;
    /**
     * 查询配置
     * @return
     */
    public entConfig query(){
        daSqlite.lock.lock();
        List<entConfig> as= daSqlite.tool.query("select * from `config` limit 0,1", entConfig.class);
        daSqlite.lock.unlock();
        if(as.isEmpty())return null;
        return as.get(0);
    }

    /**
     * 更新公司名
     * @param name
     * @return
     */
    public boolean updateName(String name){
        daSqlite.lock.lock();
        boolean a=daSqlite.tool.update("update `config` set company='"+name+"'")>0;
        daSqlite.lock.unlock();
        return a;
    }

    /**
     * 更新公司logo
     * @param logo
     * @return
     */
    public boolean updateLogo(String logo){
        try {
            long t = funSaveImage.save(logo);
            if(t<0)return false;
            daSqlite.lock.lock();
            boolean a=daSqlite.tool.update("update `config` set logo='"+t+"'")>0;
            daSqlite.lock.unlock();
            return a;
        }catch (Exception e){
            daSqlite.lock.unlock();
            throw new RuntimeException(e);
        }
    }
}
